<?php

use app\assets\ApiAsset;
use app\assets\LayerAsset;
use app\assets\MaskedInputAsset;
use app\assets\TableAsset;
use app\widgets\LinkPager;
use app\models\CardUsageSub;
use app\widgets\ManagerTableOp;
use yii\helpers\Html;
use yii\helpers\Url;
use dosamigos\datepicker\DatePicker;
use app\models\KeyMap;
/**
 * @var $this yii\web\View
 * @var $attrList app\models\CardAttr[]
 * @var $billList app\models\CardBill[]
 * @var $pagination yii\data\Pagination
 */

ApiAsset::register($this);
LayerAsset::register($this);
MaskedInputAsset::register($this);
TableAsset::register($this);

$this->title = '卡流量使用分段统计';
$this->params['breadcrumbs'][] = '财务管理';
$this->params['breadcrumbs'][] = $this->title;
?>
<?php echo Html::beginForm('?', 'get', ['class'=>'form-inline']);?>
<div class="form-group">
    <label for="search_iccid" class="sr-only">ICCID</label>
    <?php echo Html::textInput('search_iccid', Yii::$app->request->get('search_iccid'), ['id' => 'search_iccid', 'class' => 'form-control', 'placeholder' => 'ICCID', 'style' => 'width: 180px;']);?>
</div>
<div class="form-group">
    <button class="btn btn-primary btn-sm">搜索</button>
    <?php if (empty($subList)) {?>
        <button type="button" class="btn btn-success btn-sm" onclick="generateBill(this, $('[name=search_iccid]').val())">分段统计</button>
    <?php }?>
    
</div>
<?php echo Html::endForm();?>
<table class="table table-striped table-bordered table-hover">
    <thead>
    <tr>
        <th class="center">
            <label class="pos-rel">
                <input type="checkbox" class="ace" />
                <span class="lbl">编号</span>
            </label>
        </th>
        <th class="center">ICCID/MSISDN</th>
        <th class="center">代理商</th>
        <th class="center">套餐</th>
        <th class="center">周期开始时间</th>
        <th class="center">周期结束时间</th>
        <th class="center">使用流量</th>
        <th class="center">运营商流量</th>  
        <th class="center">创建时间</th>
        <th class="center">操作</th>
    </tr>
    </thead>

    <tbody>
    <?php foreach ($subList as $sub) {?>
        <tr>
            <td class="center"><label class="pos-rel"><input type="checkbox" class="ace" value="<?php echo $sub->id;?>" /><span class="lbl"><?php echo $sub->id;?></span></label></td>
            <td class="center"><?php echo empty($sub->card->iccid) ? '----' : Html::encode($sub->card->iccid);
                echo '<br />';
                echo empty($sub->card->msisdn) ? '----' : Html::encode($sub->card->msisdn);
                ?></td>
            <td class="center"><?php echo $sub->agent->username;?></td>
            <td class="center"><?php echo $sub->package->name;?></td>
            <td class="center"><?php echo Yii::$app->formatter->asDate($sub->start_date);?></td>
            <td class="center"><?php echo Yii::$app->formatter->asDate($sub->end_date);?></td>
            <td class="center"><?php echo Yii::$app->formatter->asShortSize($sub->data);?></td>
            <td class="center"><?php echo Yii::$app->formatter->asShortSize($sub->data_isp);?></td>
            <td class="center"><?php echo Yii::$app->formatter->asDate($sub->create_time);?></td>
            <td class="center">
            <?php echo ManagerTableOp::widget(['items' => [
                    ['icon' => 'fa fa-ellipsis-h','onclick' => 'view('.$sub->card_id.')','btn_class' => 'btn btn-xs btn-warning','tip' => '流量分段统计'],
                ]]);?>
            </td>
        </tr>
    <?php }?>
    </tbody>
</table>
<?php echo LinkPager::widget(['pagination' => $pagination]);?>

<!-- <script type="text/javascript">
    function deletelog(id) {
        if (!confirm('确定要删除吗？')) {
            return false;
        }
        $.getJSON('<?php echo Url::to(['/admin/finance/delete-order']);?>', {'id':id}, function(json) {
            if (callback(json)) {
                layer.msg('删除成功', function () {window.location.reload();});
            }
        });
    }
</script> -->
<script>
    /**
    * 弹出卡流量阶段使用统计    
     */
    function view(id){
        url = "<?php echo Url::to(['/admin/finance/sub-view']);?>?id="+id;
        layer.open({
        type: 2, 
        closeBtn: 2,
        title:'卡流量分段统计',
        offset: 'auto',
        area: ['750px', '800px'],
        shade: 0.5,
        shadeClose:true,
        content: url 
        }); 
    }
</script>
<script type="text/javascript">
    function setPaid(id,agentid,money,gtime,pay_type) {
        layer.confirm('确定要付款吗?', {icon: 3, title:'提示'}, function(index){
           $.getJSON('<?php echo Url::to(['/admin/finance/set-payment-paid']);?>', {id:id,agentid:agentid,money:money,gtime:gtime,pay_type:pay_type}, function (json) {
                if (callback(json)) {
                    layer.msg('提交付款申请成功，请耐心等待到账！', function () {window.location.reload();});
                }
            });
            layer.close(index);
        }); 
    }
</script>
<script>
    function generateBill(btn, iccid) {
        if (iccid == '') {
            layer.msg('请填写ICCID。', function () {});
            return;
        }
        $(btn).prop('disabled', true);
        $.getJSON('<?php echo Url::to(['/admin/finance/generate-card-usage-sub']);?>', {iccid: iccid}, function (json) {
            if (callback(json)) {
                layer.msg('流量使用分段统计成功！', function () {window.location.reload();});
            }
        });
    }

</script>
<script>
    /**
     * 调整账单金额
     * @param id
     * @param money
     */
    function modifyMoney(id, money) {
        var newMoney = window.prompt('请输入新的账单金额：', money);
        $.getJSON('<?php echo Url::to(['/admin/user/modify-commission-money']);?>', {id: id, money: newMoney}, function (json) {
            if (callback(json)) {
                layer.msg('金额已更新。', function () {window.location.reload();});
            }
        });
    }

    /**
     * 发布账单
     * @param id
     */
    function publishBill(id) {
        layer.confirm('确定要发布账单吗？', {icon: 3, title:'提示'}, function(index){
           $.getJSON('<?php echo Url::to(['/admin/user/publish-commission-bill']);?>', {id: id}, function (json) {
                if (callback(json)) {
                    layer.msg('账单已发布。', function () {window.location.reload();});
                }
            });
            layer.close(index);
        }); 
    }
</script>